
import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
import { hashHistory } from 'dva/router'
import { regionArea,getProduct,docentCommission } from './cxAllotCommissionListServ.js'
import { makeAddress } from "utils/util"

const defultState = {
    biz:{},
    area1:[],
    products:[],
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'regionArea', payload:{}});
          dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
        }
      })
    },
  },


  effects: {
    *regionArea({ payload },{ put, call, select }){
      let {biz,areaList} = yield select(state => state[tmpModule.namespace])
      const { data, success } = yield call(regionArea);
      if(success) {
        if(biz.queryForm && biz.queryForm.regionId){
          data.map((item,index)=>{
            if(biz.queryForm && biz.queryForm.regionId && biz.queryForm.regionId==item.value)
            {   
              areaList=item.children
            }
          })
        }
        yield put({ type: 'updateStore',payload:{area1:data,areaList}})
      }
    },
    *getProduct({payload},{put,call,select}){
      let {biz,products} = yield select(state => state[tmpModule.namespace])
      const { data, success } = yield call(getProduct,payload);
      if(success){
        products=data
        yield put({ type: 'updateStore',payload:{products}})
      }
    },
    //分佣
    *centCommission({ payload }, { put, call, select }){
      try{
        const { success,msg } = yield call(docentCommission,payload.id);
        let defultState = yield select(state => state[tmpModule.namespace])
        if(success){
           message.success(msg,()=>{
             defultState.biz.refreshListDatas(true)
           })
        }else{
          message.success(msg)
        }
     }catch(e){
         console.error(e)
     }
    },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
